
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<div id="procedureTab" class="tab-pane">
	<div class="panel-body" style="padding-bottom: 0px; border-bottom: 0px;">

		<div class="col-lg-12 text-right" style="padding-right: 0px;">
			<button id="createProcedureButton" class="btn btn-info btn-sm" type="button" ><i class="fa fa-plus"></i>&nbsp;&nbsp;<span class="bold">生成工序计划</span></button>
			<button id="moveProcedureButton" class="btn btn-warning btn-sm" type="button" ><i class="fa fa-exchange"></i>&nbsp;&nbsp;<span class="bold">工序移动</span></button>
			<button id="finishProcedureButton" class="btn btn-success btn-sm" type="button" ><i class="fa fa-circle-o"></i>&nbsp;&nbsp;<span class="bold">完工</span></button>
			<button id="returnProcedureButton" class="btn btn-danger btn-sm btn-redragon-adjust" type="button" ><i class="fa fa-mail-reply"></i>&nbsp;&nbsp;<span class="bold">退货</span></button>
			<%-- 
			<button id="searchButton" class="btn btn-default btn-sm" type="button"><i class="fa fa-search"></i>&nbsp;&nbsp;展开查询</button>
			--%>
		</div><br/>

		<div class="table-responsive">
			<table class="table table-stripped table-hover table-bordered border-top">

				<thead>
					<tr>
						<th width="5%">序号</th>
						<th>工序</th>
						<th>工作中心</th>
						<th>前置期（天）</th>
						<th>实际工期（天）</th>
						<th>完成数量</th>
						<th>成本费用（元）</th>
						<th>计划开工日期</th>
						<th>计划完工日期</th>
						<th>摘要</th>
						<th width="5%">状态</th>
						<th width="10%">操作</th>
					</tr>
				</thead>
				<tbody>
					
					<c:forEach items="${requestScope.prodWipTaskProcedureList}" var="data" varStatus="status">
					<tr>
						<td style="display: none;" class="taskProcedureIdClass">${data.taskProcedureId}</td>
						<td>${status.count}</td>
						<td>${data.procedureName}</td>
						<td>${data.workCenterName}</td>
						<td>${data.producePreDays}</td>
						<td>${data.finishDays}</td>
						<td>${data.finishQuantity}</td>
						<td>${data.costAmount}</td>
						<td><fmt:formatDate value="${data.startDate}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
						<td><fmt:formatDate value="${data.endDate}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
						<td>${data.memo}</td>
						<td class="statusClass" status="${data.status}">
							<c:choose>
							   <c:when test="${data.status=='WAIT'}">
							       <span class="label">${requestScope.taskProcedureStatusMap[data.status]}</span>
							   </c:when>
							   <c:when test="${data.status=='WORK'}">
							       <span class="label label-primary">${requestScope.taskProcedureStatusMap[data.status]}</span>
							   </c:when>
							   <c:when test="${data.status=='SHIFT'}">
							       <span class="label label-success">${requestScope.taskProcedureStatusMap[data.status]}</span>
							   </c:when>
							</c:choose>
						</td>
						<td>
							<div class="btn-group">
								<button class=" btn btn-xs btn-redragon-adjust" onclick="editResource('${data.procedureCode}')"><i class="fa fa-navicon"></i>&nbsp;工序资源</button>&nbsp;
								<c:if test="${data.status=='SHIFT'}">
									<button class="btn-white btn btn-xs" onclick="getProcedureShiftModal('SHIFT', '${data.taskProcedureId}');"><i class="fa fa-edit"></i>&nbsp;编辑转移单</button>&nbsp;
								</c:if>
							</div>
						</td>
					</tr>
					</c:forEach>
					
				</tbody>
				<tfoot>
					<%-- 导入页码 --%>
					<jsp:include page="../../common/pages.jsp"></jsp:include>
				</tfoot>
			</table>

		</div>

	</div>
</div>

<div id="addLineModal"></div>

<script>
	$(document).ready(function() {
		$("#createProcedureButton").click(function(){
			var alertText = "确认生成生产工序？";
			if($("#procedureTab tbody tr").length>1){
				alertText = "确认重新生成生产工序？原来的工序数据将被清空";
			}
			
			redragonJS.confirm(alertText, function(){
				createProcedure();
			});
		});
		
		//初始化按钮
		if($("#taskStatus").val()!="FINISH"){
			$("#returnProcedureButton").remove();
		}
		
		//获取需要转移的工序
		var currentTaskProcedureId = 0;
		$("#procedureTab tbody tr").each(function(index){
			if($(this).find("td[class='statusClass']").attr("status")=="WORK"){
				currentTaskProcedureId = parseInt($(this).find("td[class='taskProcedureIdClass']").text());
				return false;
			}
			
			if(currentTaskProcedureId==0&&index+1==$("#procedureTab tbody tr").length){
				currentTaskProcedureId = parseInt($(this).find("td[class='taskProcedureIdClass']").text());
			}
		});
		
		$("#moveProcedureButton").click(function(){
			//判断最后一个工序状态
			var index = $("#procedureTab tbody tr").length-1;
			var status = $("#procedureTab tbody tr:eq("+index+")").find("td[class='statusClass']").attr("status");
			if(status=="WAIT"){
				getProcedureShiftModal("SHIFT", currentTaskProcedureId);
			}else{
				redragonJS.alert("工序无法继续转移");
			}
		});
		
		$("#finishProcedureButton").click(function(){
			//判断最后一个工序状态
			var index = $("#procedureTab tbody tr").length-1;
			var status = $("#procedureTab tbody tr:eq("+index+")").find("td[class='statusClass']").attr("status");
			if(status=="WORK"){
				getProcedureShiftModal("FINISH", currentTaskProcedureId);
			}else{
				redragonJS.alert("进行到最后一个工序才能选择完工");
			}
		});
		
		$("#returnProcedureButton").click(function(){
			//判断最后一个工序状态
			var index = $("#procedureTab tbody tr").length-1;
			var status = $("#procedureTab tbody tr:eq("+index+")").find("td[class='statusClass']").attr("status");
			if(status=="SHIFT"){
				getProcedureShiftModal("RETURN", currentTaskProcedureId);
			}else{
				redragonJS.alert("已完工的生产任务才能退货");
			}
		});
		
		$("#searchButton").click(function(){
		    if($("#searchDiv").css("display")=="none"){
		        $("#searchDiv").show();
		        $("#searchButton").html('<i class="fa fa-search"></i>&nbsp;&nbsp;关闭查询');
		        $("#searchButton").addClass("btn-outline btn-warning");
		        $("#searchButton").blur();
		    }else{
		        $("#searchDiv").hide();
		        $("#searchButton").html('<i class="fa fa-search"></i>&nbsp;&nbsp;展开查询');
		        $("#searchButton").removeClass("btn-outline btn-warning");
		        $("#searchButton").blur();
		    }
		});
	});
	
	function editResource(code){
		window.open("web/prodWorkProcedure/getProdWorkProcedure?procedureCode="+code);
	}
	
	function getProcedureShiftModal(billType, id){
		$.ajax({
			type: "post",
			url: "web/prodWipTaskProcedure/getProdWipTaskProcedure",
			data: {"taskProcedureId": id, "procedureShiftType": billType, "taskCode": "${param.taskCode}"},
			async: false,
			dataType: "html",
			cache: false,
			success: function(data){
				if(data!=""){
					$("#addLineModal").html(data);
					$('#addLineDiv').modal('show');
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
	
	/*
	function deleteSysUser(id) {
		redragonJS.confirm("确认删除数据？", function(){
			deleteLine(id);
		});
	}
	
	function deleteLine(id){
		$.ajax({
			type: "post",
			url: "web/prodWorkCraftProcedure/deleteProdWorkCraftProcedure",
			data: {"craftProcedureId": id, "craftCode": "${param.craftCode}"},
			async: false,
			dataType: "html",
			cache: false,
			success: function(data){
				var json = JSON.parse(data);
				if(json.result=="success"){
					redragonJS.close();
					getLineTab("${param.craftCode}");
				}else{
					redragonJS.alert("删除工序行错误");
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
	*/
	
	function createProcedure(){
		redragonJS.loading("line-ibox-content");
		
		$.ajax({
			type: "post",
			url: "web/prodWipTaskProcedure/createWipTaskProcedureAjax",
			data: {"taskCode": "${param.taskCode}"},
			async: true,
			dataType: "json",
			cache: false,
			success: function(data){
				redragonJS.removeLoading("line-ibox-content");
				if(data.result=="success"){
					redragonJS.close();
					getProcedureLineTab("${param.taskCode}");
				}else if(data.result=="hint"){
					redragonJS.alert(data.message);
				}else{
					redragonJS.alert("创建生产工序错误");
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
</script> 